<!--登录页-->
<template>
<div class="login-contain">
  <div class="login-logo"><img src="~images/logo/logo1.png"></div>
  <div class="login-wrap">
    <div class="login-row" :class={cur:nameFocus}>
      <span class="label">账号</span>
      <input class="login-ipt" type="text" id="userName"
             placeholder="请输入账号" 
             v-model.trim = "userName" 
             @focus="nameFocus = true"
             @blur="nameFocus = false"
             @keyup.enter = "login">
    </div>
    <div class="login-row" :class={cur:pwFocus}>
      <span class="label">密码</span>
      <input class="login-ipt" type="password" id="userPwd"
             placeholder="请输入密码" 
             v-model.trim = "userPwd"
             @focus="pwFocus = true"
             @blur="pwFocus = false"
             @keyup.enter = "login">
    </div>
    <input type="button" value="链接未来" class="login-btn" @click="login" id="subBtn">
    <div class="line-box">
      <router-link to="/user/register" class="lf">新用户注册</router-link>
      <router-link to="/user/password?forget=1" class="rit">忘记密码</router-link>
    </div>
  </div>
  <!-- <div class="exper">
    <div>您还可以，<a class="link">快速体验</a></div>
    <p>登录代表您同意本协议 <i class="iconfont icon-explain"></i></p>
  </div> -->
</div>
</template>
<script>
export default {
  data() {
    return {
      userName:'',
      userPwd:'',
      nameFocus:false,
      pwFocus:false,
    }
  },
  created(){
    this.savePw();
  },
  methods:{
    //保存密码
    savePw: function() {
      if (localStorage.userinfo) {
        var use = JSON.parse(localStorage.userinfo);
        this.userName = use.LogonUser;
        this.userPwd = use.Pwd;
      }
    },
    getQueryString(name){
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    btnC(){
      $("#subBtn").val("链接未来");
      $("#userName , #userPwd").removeAttr("readonly", false);
      $("#subBtn").removeAttr("disabled", false);
    },
    login(){
      // 用户名
      if (!this.userName) {
        this.Toast({
          message:'请输入用户名'
        })
        return;
      };
      // 密码
      if (!this.userPwd) {
        this.Toast({
          message:'密码不能为空'
        })
        return;
      };
      var self = this;
      var code = this.getQueryString('code');
      var params = {
          LogonUser: this.userName,
          Pwd: this.userPwd,
          Code: code
      };
      $("#subBtn").val("正 在 登 录 中 ...");
      $("#userName , #userPwd").attr("readonly", true);
      $("#subBtn").attr("disabled", "disabled");
      this.Ajax.post('Login', params).then(function(result) {
        console.log(result)
        if (result.IsSuccess && result.Data) {
          if (window.plus) {
              plusReady();
          } else {
              document.addEventListener('plusready', plusReady, false);
          }
          function plusReady() {
              //Android返回键监听事件
              plus.key.addEventListener('backbutton', function () {
                  plus.nativeUI.confirm('确认退出？', function (e) {
                      if (e.index > 0) {
                          plus.runtime.quit();
                      }
                  });
              });
          }

          result.Data.Pwd = self.userPwd;
          var allData = result.Data;
          if (allData.UserBizType != "Driver" && allData.UserBizType != "Warehouse") {
            self.Toast({
              message: "账号或密码错误！"
            });
            self.btnC();
            return false;
          }
          localStorage.userinfo = JSON.stringify(allData);
          if (allData.UserType == '2') {
            self.$router.push({
              path: "/indexMan"
            });
          } else {
            if (allData.UserBizType == 'Salesman') {
              self.$router.push({
                path: "/news"
              });
            } else {
              self.$router.push({
                path: "/order"
              });
            }
          }
        } else {
          self.Toast({
            message:result.ErrorMessage || '操作失败，请稍后重试'
          });
        }
        self.btnC();
      });
    }
  }
}
</script>